import { TableInfo } from "@/entities/tableInfo.entity";
import createFile from "./createFile";

/**
 * 创建dto文件
 * @param name 
 * @param remain 
 */
const createDtoFile = async (data: TableInfo) => {
    const { name, fieldInfoList = [], indexInfoList = [] } = data;
    //表名下划线转小驼峰
    const smallName = name.replace(/_(.)/g, (a: string, b: string) => `${b.toLocaleUpperCase()}`);

    //表名下划线转大驼峰
    const bigName = name.replace(/(?:^|_)(\w)/g, (_, c) => c.toUpperCase());


    const dtoImport = `
import { IsNotEmpty, IsString,  IsOptional, IsInt,  MaxLength,ArrayMinSize,IsArray ,ValidateNested} from 'class-validator';
import { Type  } from 'class-transformer';
import { BaseRequestListDto } from '@/dto/baseRequestList.dto';
        
        `


    // 生成createDco
    let createDto = `
export class Create${bigName}Dto {
        `
    let updateDto = `
export class Update${bigName}Dto {    
        `
    let queryDto = `
export class Query${bigName}Dto extends BaseRequestListDto {
        `

    let deleteDto = `
export class Delete${bigName}Dto {
    @IsNotEmpty({ message: 'ID列表不能为空' })
    @IsInt({ each: true, message: '每个ID必须是整数' })
    ids!: number[];
}
        
        `
    let selectOneDto = `
export class SelectOne${bigName}Dto {
    @IsNotEmpty({ message: 'ID不能为空' })
    id!: number;
}
        
        
        `
    let sortItem = `
/**
class Sort${bigName}ItemDto {
    @IsInt({ message: 'id必须是整数' })
    @IsNotEmpty({ message: 'ID不能为空' })
    id!: number;

    @IsInt({ message: 'sort必须是整数' })
    @IsNotEmpty({ message: '排序不能为空' })
    sortOrder!: number;
}`



    let sortDto = `
export class Sort${bigName}Dto {
    @IsArray({ message: 'list必须是数组' })
    @ArrayMinSize(1, { message: 'list不能为空数组' })
    @ValidateNested({ each: true }) // 校验每个数组项
    @Type(() => Sort${bigName}ItemDto) // 必须配合class-transformer
    list!: Sort${bigName}ItemDto[];
}*/
        
        `

    let responseDto = `
export class ${bigName}ResponseDto{    
        `

    fieldInfoList.forEach(item => {
        /**当字段可为空，或有默认值的，可以为空 */
        const isCanEmpty = item.isNull || item.defaultValue;
        const isNumber = ["tinyint", 'int', 'integer', 'smallint', 'mediumint', 'bigint', 'decimal', 'numeric'].indexOf(item.dataType as string) != -1;
        // 字段名下划线转小驼峰
        const propertyName = item.code.replace(/_(.)/g, (a: string, b: string) => `${b.toLocaleUpperCase()}`);

        if (item.isInsert) {
            createDto += `
    /**${item.name}*/
    ${isCanEmpty ? `@IsOptional()` : `@IsNotEmpty({ message: '${item.name}不能为空' })`}
    ${propertyName}${isCanEmpty ? "?" : "!"}:${isNumber ? "number" : "string"}; 
            `
        }
        if (item.isEdit) {
            updateDto += `
    /**${item.name}*/
    @IsOptional()
    ${propertyName}?:${isNumber ? "number" : "string"}; 
            `
        }
        if (item.isQuery) {
            queryDto += `
    /**${item.name}*/
    @IsOptional()
    ${propertyName}?:${isNumber ? "number" : "string"}; 
            `
        }

        responseDto += `
    /**${item.name}*/
    @IsOptional()
    ${propertyName}?:${isNumber ? "number" : "string"}; 
            `
    })
    createDto += `
}`;
    updateDto += `
}`;
    queryDto += `
}`;
    responseDto += `
}`;


    const dtoStr = dtoImport + createDto + updateDto + queryDto + deleteDto + selectOneDto + responseDto + sortItem + sortDto
    await createFile(dtoStr, `dto/${smallName}.dto.ts`)
    return dtoStr
}
export default createDtoFile